import { toReversed } from '/@/utils/array';
import { state } from './state';

/**
 * 设置属性可读/可写/隐藏 扩展属性
 */
function setExtendAttributes(
  fptApproverAttrCfgList?: FlowPathDto.FptApproverAttrCfgDTO[],
  myHandleStatus?: string,
) {
  console.log('fptApproverAttrCfgList', fptApproverAttrCfgList);
  fptApproverAttrCfgList?.forEach((item) => {
    const attribute = state.attributes?.find((subItem) => subItem.id === item.fprAttributeId);
    //from：为空或者send
    //只有当从我的审批进去才能审批，从审批记录不能审批
    if (myHandleStatus === '1') {
      if (attribute) {
        attribute.readonlyAble = item.readonlyAble || 1;
        attribute.editAble = item.editAble || 2;
        attribute.hiddenAble = item.hiddenAble || 2;
      }
    } else {
      //在已处理和已抄送中
      //默认是只读
      if (attribute) {
        attribute.readonlyAble = 1;
        attribute.editAble = 2;
        attribute.hiddenAble = item.hiddenAble || 2;
      }
    }
  });
}
/**
 * 详情页默认属性
 */
function setDefaultExtendAttributes() {
  state.attributes?.forEach((item) => {
    item.readonlyAble = 1;
    item.editAble = 2;
    item.hiddenAble = 2;
  });
}

/**
 * 查找不同状态属性对应的权限
 * @returns 返回属性对应的控制权限
 */
function findExtendAttributes(myHandleStatus: string) {
  console.log('findExtendAttributes', myHandleStatus);
  //（1-待处理，2-已处理，4-已收到-抄送人, 3-历史记录）
  switch (myHandleStatus) {
    case '1':
      return findExtendAttributesByUnHandle();
    case '2':
      return findExtendAttributesByHandled();
    case '4':
      return findExtendAttributesByCopy();
    case '3':
      return findExtendAttributesBySelf();
  }
}

/**
 * 在历史记录找是自己的第一个权限设置
 * 产品的要求
 */
function findExtendAttributesBySelf() {
  if (!state.record || !state.record.approvers) {
    return;
  }
  let approve;
  for (let i = 0; i < state.record.approvers.length; i++) {
    const item = state.record.approvers[i];
    if (item.approverUaaUserId != '' && item.approverUaaUserId === state.uaaUserId) {
      approve = item;
    } else {
      approve = item?.recordApproverVSetList?.find(
        (subItem) =>
          subItem.approverUaaUserId != '' && subItem.approverUaaUserId === state.uaaUserId,
      );
      if (approve) {
        break;
      }
    }
  }
  return approve && approve.fptApproverAttrCfgList;
}

/**
 * 分三种情况：
 * 1. 待处理-只有一个
 */
function findExtendAttributesByUnHandle() {
  console.log('findExtendAttributesByUnHandle');
  const approve = state.record?.approvers?.find((item) => item.reviewStatus === 1);
  const childApprove = approve?.recordApproverVSetList?.find((item) => item.reviewStatus === 1);
  return childApprove && childApprove.fptApproverAttrCfgList;
}

/**
 * 2. 已处理
 * @returns 是否是已处理用户
 */
function isUserHandled() {
  const approve = toReversed(state.record?.approvers).find(
    (item) =>
      item.reviewStatus === 2 &&
      item.approverUaaUserId != '' &&
      item.approverUaaUserId === state.uaaUserId,
  );
  if (approve) {
    return true;
  }

  const childApprove = toReversed(approve?.recordApproverVSetList).find(
    (item) =>
      item.reviewStatus === 2 &&
      item.approverUaaUserId != '' &&
      item.approverUaaUserId === state.uaaUserId &&
      item.flowType === 1,
  );
  if (childApprove) {
    return true;
  }
  return false;
}
/**
 * 2. 已处理-是自己的&审批通过&最新的记录
 */
function findExtendAttributesByHandled() {
  const approve = toReversed(state.record?.approvers).find(
    (item) =>
      item.reviewStatus === 2 &&
      item.approverUaaUserId != '' &&
      item.approverUaaUserId === state.uaaUserId,
  );
  const current = approve && approve.fptApproverAttrCfgList;
  if (current) {
    return current;
  }

  const childApprove = toReversed(approve?.recordApproverVSetList).find(
    (item) =>
      item.reviewStatus === 2 &&
      item.approverUaaUserId != '' &&
      item.approverUaaUserId === state.uaaUserId &&
      item.flowType === 1,
  );
  return childApprove && childApprove.fptApproverAttrCfgList;
}

/**
 * 3. 已收到
 * @returns 是否是已抄送用户
 */
function isUserCopy() {
  const approve = toReversed(state.record?.approvers).find(
    (item) =>
      item.reviewStatus === 2 &&
      item.approverUaaUserId != '' &&
      item.approverUaaUserId === state.uaaUserId,
  );

  if (approve) {
    return true;
  }

  const childApprove = toReversed(approve?.recordApproverVSetList).find(
    (item) =>
      item.reviewStatus === 2 &&
      item.approverUaaUserId != '' &&
      item.approverUaaUserId === state.uaaUserId &&
      item.flowType === 2,
  );
  if (childApprove) {
    return true;
  }
  return false;
}

/**
 * 3. 已收到-是自己的&抄送&最新的记录
 */
function findExtendAttributesByCopy() {
  const approve = toReversed(state.record?.approvers).find(
    (item) =>
      item.reviewStatus === 2 &&
      item.approverUaaUserId != '' &&
      item.approverUaaUserId === state.uaaUserId,
  );
  const current = approve && approve.fptApproverAttrCfgList;
  if (current) {
    return current;
  }

  const childApprove = toReversed(approve?.recordApproverVSetList).find(
    (item) =>
      item.reviewStatus === 2 &&
      item.approverUaaUserId != '' &&
      item.approverUaaUserId === state.uaaUserId &&
      item.flowType === 2,
  );
  return childApprove && childApprove.fptApproverAttrCfgList;
}

/**
 * 添加审批的第一个节点
 */
function addFirstNode(releaseUserName?: string, releaseTime?: number, releaseUserPhone?: string) {
  state.record?.approvers?.splice(0, 0, {
    approverUserName: releaseUserName,
    reviewStatus: 0,
    updateTime: releaseTime,
    approverUserPhone: releaseUserPhone,
    on: 1,
  });
}

/**
 * 最新审批节点
 * @param currentUser 当前用户的审uuid
 * @returns 是否能审批 和 approve的id
 */
function isCurrentUserApprove(currentUser?: string) {
  if (!currentUser) {
    return { approve: false, id: undefined };
  }
  const approve: FlowPathDto.ApproverDto | undefined = toReversed(state.record?.approvers)?.find(
    (item) => item.on === 1,
  );
  if (!approve) {
    return { approve: false, id: undefined };
  }
  if (approve.recordApproverVSetList && approve.recordApproverVSetList.length > 0) {
    const childApproveFirst = approve.recordApproverVSetList[0];
    if (childApproveFirst.reviewStatus === 1) {
      if (childApproveFirst.multiApproveType === 1) {
        if (
          childApproveFirst.approverUaaUserId &&
          childApproveFirst.approverUaaUserId === currentUser
        ) {
          return { approve: true, id: childApproveFirst.id };
        }
      } else if (
        childApproveFirst.multiApproveType === 2 ||
        childApproveFirst.multiApproveType === 3
      ) {
        const childApprove = approve.recordApproverVSetList.find(
          (item) => item.approverUaaUserId === currentUser,
        );
        if (childApprove) {
          return { approve: true, id: childApprove.id };
        }
      }
    }
  }
  return { approve: false, id: undefined };
}

/**
 * 判断是不是专有钉钉PC端
 */
function isDingTalkPCOrBrowser() {
  if (navigator.userAgent.includes('TaurusApp')) {
    if (navigator.userAgent.search(/(iphone|ipad|ipod)/i) >= 0) {
      return false;
    } else if (navigator.userAgent.search(/android/i) >= 0) {
      return false;
    } else {
      return true;
    }
  }
  return true;
}

/**
 * 判断是不是专有钉钉
 */
function isDingTalk() {
  if (navigator.userAgent.includes('TaurusApp')) {
    return true;
  }
  return false;
}

export {
  setExtendAttributes,
  setDefaultExtendAttributes,
  findExtendAttributes,
  addFirstNode,
  isCurrentUserApprove,
  isUserCopy,
  isUserHandled,
  isDingTalkPCOrBrowser,
  isDingTalk,
};
